//
//  SettingViewController.m
//  PIC
//
//  Created by luffy on 14-10-21.
//  Copyright (c) 2014年 panchuyou. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingModel.h"
#import "AboutOurViewController.h"

#import "SDWebImageManager.h"
#import "FilePathManager.h"
#import "UIButton+NavButton.h"



@interface SettingViewController ()
@property (nonatomic,retain) NSArray * array;
@end

@implementation SettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.navigationItem.title = @"设置";
        

    }
    return self;
}


- (void)backClick:(UIButton *)btn
{
    
    if (_delegate && [_delegate respondsToSelector:@selector(settingViewControllerSelectRootViewFirstIndex)]) {
        [_delegate settingViewControllerSelectRootViewFirstIndex];
    }
    [self.navigationController popViewControllerAnimated:YES];}
- (void)viewDidLoad
{
    [super viewDidLoad];
    _array = [[NSArray alloc] initWithObjects:@"清除缓存",@"关于我们",@"版本信息", nil];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *leftButton = [UIButton addNavButtonWithNormalImageString:@"back" andHeightLightedImageString:@"back_highlighted"];
    CGSize leftButtonSzie = leftButton.currentImage.size;
    leftButton.frame = (CGRect){CGPointZero,leftButtonSzie};
    //leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [leftButton addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftBarButton = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
    self.navigationItem.leftBarButtonItem = leftBarButton;

    
    UIImage * img = [UIImage imageNamed:@"SettingLogo"];
    UIImageView * imgView = [[UIImageView alloc] initWithImage:img];
    imgView.frame = CGRectMake(0, 0, 320, 217);
    [self.view addSubview:imgView];
    [imgView release];
  
   
    
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,217, 320, 135) style:UITableViewStylePlain];
    tableView.bounces = NO;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [tableView release];
    
//    UIButton * weatherBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    weatherBtn.frame = CGRectMake(5, 386, 100, 100);
//    [weatherBtn setImage:[UIImage imageNamed:@"Oval_icon_net_72"] forState:UIControlStateNormal];
//    [weatherBtn addTarget:self action:@selector(weatherClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:weatherBtn];
//    
//    UILabel * weatherLabel = [[UILabel alloc] initWithFrame:CGRectMake(38, 530, 100, 20)];
//    weatherLabel.text = @"天气";
//    weatherLabel.font = [UIFont systemFontOfSize:15];
//    [self.view addSubview:weatherLabel];
//    [weatherLabel release];
//    
//    UIButton * collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    collectBtn.frame = CGRectMake(110, 450, 100, 100);
//    [collectBtn setImage:[UIImage imageNamed:@"icon_star"] forState:UIControlStateNormal];
//    [collectBtn addTarget:self action:@selector(collectClick) forControlEvents:UIControlEventTouchUpInside];
//    //[self.view addSubview:collectBtn];
//    
//    UILabel * collectLabel = [[UILabel alloc] initWithFrame:CGRectMake(143, 530, 100, 20)];
//    collectLabel.text = @"收藏";
//    collectLabel.font = [UIFont systemFontOfSize:15];
//    //[self.view addSubview:collectLabel];
//    [collectLabel release];
//    
//    UIButton * ourBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    ourBtn.frame = CGRectMake(215, 386, 100, 100);
//    [ourBtn setImage:[UIImage imageNamed:@"aboutOur"] forState:UIControlStateNormal];
//    [ourBtn addTarget:self action:@selector(ourClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:ourBtn];
//    
//    UILabel * ourLabel = [[UILabel alloc] initWithFrame:CGRectMake(233, 530, 100, 20)];
//    ourLabel.text = @"关于我们";
//    ourLabel.font = [UIFont systemFontOfSize:15];
//    [self.view addSubview:ourLabel];
//    [ourLabel release];
    
    

}
- (void)ourClick
{
    AboutOurViewController * abVC = [[AboutOurViewController alloc] init];
    [self.navigationController pushViewController:abVC animated:YES];
    [abVC release];
}
- (void)collectClick
{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        SDImageCache *imageCache = [SDImageCache sharedImageCache];
        
        [imageCache clearDisk];
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"内存已清理" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
        NSFileManager *fm = [NSFileManager defaultManager];
        
        NSArray *listFileArray = [fm subpathsAtPath:[FilePathManager webCachePath]];
        
        for (NSString *fileStr in listFileArray) {
            
            [fm removeItemAtPath:[FilePathManager imagePathCache:fileStr] error:nil];
        }

    }
    if(indexPath.row == 1)
    {
        AboutOurViewController * abVC = [[AboutOurViewController alloc] init];
        [self.navigationController pushViewController:abVC animated:YES];
        [abVC release];
    }
    }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"]autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSString * temstr = [_array objectAtIndex:indexPath.row];
    cell.textLabel.text = temstr;
//    if (indexPath.row == 1) {
//        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
//        NSString * wifistatus = [defaults objectForKey:@"switchStatus"];
//        int s = [wifistatus intValue];
//        NSLog(@"%d",s);
//        UISwitch * switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
//        [switchView addTarget:self action:@selector(changeWifi:) forControlEvents:UIControlEventValueChanged];
//        switchView.on = s;
//        cell.accessoryView = switchView;
//        [switchView release];
//    }
    
    if (indexPath.row == 2) {
        cell.detailTextLabel.text = @"版本1.0.0";
    }
    return cell;
}

- (void)changeWifi:(UISwitch *)changeSwitch
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    if ([changeSwitch isOn]) {
        [defaults setObject:@"1" forKey:@"switchStatus"];
    }else{
        [defaults setObject:@"0" forKey:@"switchStatus"];
    }
    
    [defaults synchronize];
}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
